package com.beyondhost.openapi.demo.Controller;

import com.beyondhost.openapi.demo.Helpers.CommonRequestHelper;
import com.beyondhost.openapi.demo.Helpers.JsonHelper;
import com.beyondhost.openapi.demo.Helpers.SignHelper;
import com.beyondhost.openapi.demo.RequestModel.HotelSearchRequest;
import com.beyondhost.openapi.demo.RequestModel.OpenApiRequestCommonModel;
import com.beyondhost.openapi.demo.RequestModel.OrderByRequest;
import com.beyondhost.openapi.demo.ResponseModel.HotelSearchResponse;
import com.beyondhost.openapi.demo.ResponseModel.OpenApiResponseCommonModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.http.HttpHost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.eclipse.jetty.client.HttpProxy;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/hotel")
public class HotelController {

    private final static String appKey = "6AF0062B-9C6D-4796-8760-803779CF7E48";

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Object SearchHotelWithRoomPriceAndRoomCount() throws Exception {
        URI uri = new URI("https://cpmsinterface.test.beyondh.com:7898/");

        //URI uri = new URI("http://cpmsinterface.test.beyondh.com:7897/");

        //预抵时间
        Calendar calendar = Calendar.getInstance();
        Date now = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String arriveTime = format.format(now);

        //预离时间
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String departureTime = format.format(calendar.getTime());

        HotelSearchRequest searchRequest = new HotelSearchRequest();
        searchRequest.setArriveTime(arriveTime);
        searchRequest.setDepartureTime(departureTime);
        searchRequest.setHotelIds(new long[]{660247752032257L});
        searchRequest.setCheckinType("Normal");
        searchRequest.setRoomTypeIds(new String[]{"TR"});
        searchRequest.setRoomStatuses(new String[]{});
        searchRequest.setMemberLevels(new String[]{});
        searchRequest.setPhysicalRoomTypeOnly(false);
        searchRequest.setBasicInfoOnly(false);
        searchRequest.setIncludeDetailCounts(true);
        searchRequest.setIncludePrices(true);
        searchRequest.setIncludeRoomCounts(true);
        searchRequest.setRateCode("");
        searchRequest.setContractorLevels(new String[]{});
        searchRequest.setSearchType("");
        searchRequest.setOrderByRequests(new OrderByRequest[]{});
        searchRequest.setPageIndex(1);
        searchRequest.setPageSize(2);

        String bizContent = JsonHelper.SerializeObject(searchRequest);

        OpenApiRequestCommonModel requestCommonModel = CommonRequestHelper.GetCommonRequestModel("Hotel.SearchHotelWithRoomPriceAndRoomCount", bizContent);
        SignHelper.Sign(requestCommonModel, appKey);

        HttpHeaders headers = new HttpHeaders();
        headers.add("domain", "cpmsinterface");
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        RequestEntity<OpenApiRequestCommonModel> requestEntity = new RequestEntity<OpenApiRequestCommonModel>(requestCommonModel, headers, HttpMethod.POST, uri);

        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                })
                .build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                //.setProxy(new HttpHost("127.0.0.1",8888))
                .build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        RestTemplate template = new RestTemplate(factory);
        ResponseEntity<String> responseMessage = template.exchange(requestEntity, String.class);
        OpenApiResponseCommonModel<HotelSearchResponse> result = JsonHelper.DeserializeObject(responseMessage.getBody(), new TypeReference<OpenApiResponseCommonModel<HotelSearchResponse>>() {
        });

        return result;
    }

}
